<template>
	<view>
		<navber id="head-navber" :navbarData="navbarData"></navber>
		<view class="card-list">
			<view class="card-item" v-for="(item,index) in card_list" :key="index">
				<card-item :cardData="item" @openCard="onOpenCard()" @compCard="onCompCard()">
				</card-item>
			</view>
		</view>
		<view class="end"></view>
	</view>
</template>
<script>
	import navber from '@/components/navber/index';
	import cardItem from "@/components/card/index";
	// pages/index/index.js
	const app = getApp();

	const Api = require('../../../api/index.js');

	const Config = require('../../../config/index.js');
	export default {
		components: {
			navber,
			cardItem
		},
		data() {
			return {
				navbarData: {
					title: '我的卡片' //顶部标题
				},
				//导航栏
				imgUrl: Config.imgUrl,
				card_list: [],
				class: 1
			}
		},
		onLoad(e) {
			
			if (e.class) {
				this.setData({
					class: e.class
				});
				console.log(e);
				if (parseInt(e.class) == 2) {
					this.navbarData.title = "我的礼盒";
				} else {
					this.navbarData.title = "我的卡片";
				}
			}
		},
		onShow() {
			this.getGameCardList();
		},
		onPullDownRefresh: function() {
			this.getGameCardList();
			uni.stopPullDownRefresh();
		},
		methods: {
			getGameCardList() {
				var json = {
					class: this.class
				};
				Api.getGameCardList(json).then((res) => {
					if (res.code == 1) {
						if(res.data){
							this.setData({
								card_list: res.data
							});
						}else{
							this.setData({
								card_list: []
							});
						}
					}
				});
			},
			onOpenCard(e) {
				// console.log(e);
				let that = this;
				var json = {
					type: e.detail.type
				};
				Api.openGameCard(json).then((res) => {
					if (res.code == 1) {
						that.getGameCardList();
					}
				});
			},
			onCompCard(e) {
				// console.log(e);
				let that = this;
				var json = {
					type: e.detail.type
				};
				Api.comGameCard(json).then((res) => {
					if (res.code == 1) {
						uni.redirectTo({
							url: '/pages/store/card/index?class=2'
						});
					}
				});
			},
			
		}
	}
</script>

<style>
	@import './index.css';
</style>
